package casbininit

import (
	"go-project/common"
	"os"

	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v2"
)

var (
	Enforcer *casbin.Enforcer
)

func SetupCasbinEnforcer() error {
	//获取项目根目录
	dir, _ := os.Getwd()
	modelPath := dir + "/config/rbac_model.conf"
	adapter, _ := gormadapter.NewAdapterByDB(common.DB)
	var errC error

	Enforcer, errC = casbin.NewEnforcer(modelPath, adapter)

	if errC != nil {
		return errC
	} else {
		// 必须执行
		Enforcer.LoadPolicy()
		Enforcer.EnableLog(true)
		return nil
	}
}
